package com.newviews.tcmip.web.controller;

import com.newviews.tcmip.common.config.ConfigProperties;
import com.newviews.tcmip.common.constant.CommonConsts;
import com.newviews.tcmip.common.model.PageModel;
import com.newviews.tcmip.common.model.ResultModel;
import com.newviews.tcmip.common.utils.ExportExcelUtils;
import com.newviews.tcmip.common.utils.UUIDGenerator;
import com.newviews.tcmip.pojo.vo.EtcmCommonBaseInfoRespVO;
import com.newviews.tcmip.pojo.vo.export.EtcmCommonExportReqVO;
import com.newviews.tcmip.service.inf.EtcmDiseaseExServiceInf;
import com.newviews.tcmip.service.inf.EtcmIngrExServiceInf;
import com.newviews.tcmip.service.inf.EtcmIngrServiceInf;
import com.newviews.tcmip.web.vo.etcmherb.download.EtcmIngrByHerbExportReqVO;
import com.newviews.tcmip.web.vo.etcmherb.download.EtcmIngrDetailsExportReqVO;
import com.newviews.tcmip.web.vo.etcmingr.EtcmTargetExportReqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 组成成分 前端控制器
 * </p>
 *
 * @author lxc
 * @since 2024-02-29
 */
@RestController
@RequestMapping("/etcm/ingr/download")
@Api(tags = "组成成分下载")
public class EtcmIngrDownLoadController {
    @Resource
    private EtcmIngrExServiceInf etcmIngrExServiceInf;

    @Resource
    private ConfigProperties configProperties;
    @Resource
    private EtcmDiseaseExServiceInf etcmDiseaseExServiceInf;

    @PostMapping("")
    @ApiOperation(value = "成分信息：下载",notes="ingredient")
    public void downloadIngredient(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.downloadIngredient(reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode(), reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/details")
    @ApiOperation(value = "成分信息：下载",notes="Basic information：ingredient information")
    public void downloadIngredientInformation(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> pxDetails = etcmIngrExServiceInf.downloadIngredientInformation(reqVO);
            commonDownload(response, reqVO, pxDetails);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }


    @ApiOperation(value = "成分构成：相关靶向下载",notes="information composition：Target")
    @PostMapping("/target")
    public void downloadTarget(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmTargetExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.getTargetDownload(reqVO.getType(),reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode(), reqVO.getIngrId(), reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




    @ApiOperation(value = "成分组学信息：下载",notes="omics data records: experimental records")
    @PostMapping("/experimental/records")
    public void downloadExperimentalRecords(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.getDownloadExperimentalRecords(reqVO.getContent(), reqVO.getLangCode(), reqVO.getUiTypeCode(), reqVO.getIngrId(), reqVO.getSortType());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "成分组合用药关系：协同成分下载",notes="combinational landscape: Synergistic ingredients")
    @PostMapping("/synergistic/ingredients")
    public void downloadSynergisticIngredients(
            @ApiIgnore HttpServletResponse response,
            @RequestBody  EtcmTargetExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.downloadSynergisticIngredients(reqVO.getIngrId(),reqVO.getSortType(),reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "成分组合用药关系：相关的疾病下载",notes="combinational landscape: associated diseases")
    @PostMapping("/associated/diseases/page")
    public void downloadAssociatedDiseases(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.downloadAssociatedDiseases(reqVO.getIngrId(),reqVO.getSortType(),reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "成分：相关的处方下载",notes="Related Prescription")
    @PostMapping("/px")
    public void downloadPx(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrDetailsExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.downloadPx(reqVO.getIngrId(),reqVO.getSortType(),reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @ApiOperation(value = "成分：相关的中草药下载",notes="Related herb")
    @PostMapping("/herb")
    public void downloadHerb(
            @ApiIgnore HttpServletResponse response,
            @RequestBody @Valid EtcmIngrByHerbExportReqVO reqVO) {
        try {
            List<Map<String, String>> list = etcmIngrExServiceInf.downloadHerb(reqVO.getIngrId(),reqVO.getSortType(),reqVO.getContent(),reqVO.getLangCode(),reqVO.getUiTypeCode(),reqVO.getHerbFlavorCode(),reqVO.getHerbPropertyCode(),reqVO.getHerbMeridiansCode());
            etcmDiseaseExServiceInf.getDownload(response,reqVO.getUiTypeCode(),reqVO.getLangCode(),reqVO.getExportType(),list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    private void commonDownload(@ApiIgnore HttpServletResponse response, @RequestBody @Valid EtcmCommonExportReqVO reqVO, List<Map<String, String>> pxDetails) throws IOException {
        String path;
        String filename = "";
        File file=new File(configProperties.getFileExportPath());
        if(!file.exists()){
            file.mkdirs();
        }
        if(reqVO.getExportType().equals(CommonConsts.STR_COMMON_YES)){
            filename = UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_1;
            path= configProperties.getFileExportPath()+ filename;
            ExportExcelUtils.writeListOfMapsToCSV(pxDetails, path);
        }else {
            filename = UUIDGenerator.getDateNumber()+CommonConsts.EXPORT_FILE_FORMAT_2;
            path= configProperties.getFileExportPath()+ filename;
            ExportExcelUtils.writeListOfMapsToTxt(pxDetails, path);
        }
        InputStream stream = new FileInputStream(path);
        response.setHeader("Content-Disposition", "attachment;"+"filename=" + new String(filename.getBytes(), "utf-8"));
        ServletOutputStream out = response.getOutputStream();
        byte[] buff = new byte[1024];
        int length = 0;
        while ((length = stream.read(buff)) > 0) {
            out.write(buff, 0, length);
        }
        stream.close();
        out.close();
        out.flush();
    }

}
